<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
class Test1026Controller extends Controller
{
    public function login(){
        return view('admin/index/test1026login');
    }
    public function logindo(Request $request){
        $validator = Validator::make($request->all(), [
            'username' => 'required',
            'password' => 'required',
        ],[
            'username.required' => "<script>alert('用户名必须填写')</script>",
            'password.required'  => "<script>alert('密码必须填写')</script>",
        ]);

        if ($validator->fails()) {
            header("refresh:0;url=login");
            return $validator->errors()->first();
        }
        $data['username'] = $request['username'];
        $data['password'] = $request['password'];
        $res = auth()->attempt($data);
        if($res){
            echo "<script>alert('登录成功')</script>";
            header("refresh:0;url=index");
        }else{
            echo "<script>alert('登录失败')</script>";
            header("refresh:0;url=login");
        }
    }
    public function index(){
        $data = DB::select("select * from user3");
        $res = json_decode(json_encode($data),true);
        $man = 0;
        $woman = 0;
        foreach($res as $v){
            if($v['sex'] == '男'){
                $man ++;
            };
            if($v['sex'] == '女'){
                $woman ++;
            };
        }
        return view('admin/index/test1026index',['man'=>$man,'woman'=>$woman]);
    }
    public function Map(){
        return view('admin/index/test1026map');
    }
    public function GetMap(Request $request){
        $ak = "hfVwGB6EZahtNlQlrm2RfdrGXswq3oOt";
        $address = $request['address'];
        $url = file_get_contents("http://api.map.baidu.com/geocoding/v3/?address=$address&output=json&ak=$ak");
        $data = json_decode($url,true);
        $res['lng'] = $data['result']['location']['lng'];
        $res['lat'] = $data['result']['location']['lat'];
        return view('admin/index/test1026getmap',['res'=>$res,'address'=>$address]);
    }
}
